package com.ruoyi.point.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.point.domain.PointFlow;
import com.ruoyi.point.service.IPointFlowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分流水Controller
 * 
 * @author hy
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/point/flow")
public class PointFlowController extends BaseController
{
    @Autowired
    private IPointFlowService pointFlowService;

    /**
     * 查询积分流水列表
     */
    @PreAuthorize("@ss.hasPermi('point:pointFlow:list')")
    @GetMapping("/list")
    public TableDataInfo list(PointFlow pointFlow)
    {
        startPage();
        List<PointFlow> list = pointFlowService.selectPointFlowList(pointFlow);
        return getDataTable(list);
    }

    /**
     * 导出积分流水列表
     */
    @PreAuthorize("@ss.hasPermi('point:pointFlow:export')")
    @Log(title = "积分流水", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PointFlow pointFlow)
    {
        List<PointFlow> list = pointFlowService.selectPointFlowList(pointFlow);
        ExcelUtil<PointFlow> util = new ExcelUtil<PointFlow>(PointFlow.class);
        return util.exportExcel(list, "积分流水数据");
    }

    /**
     * 获取积分流水详细信息
     */
    @PreAuthorize("@ss.hasPermi('point:pointFlow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointFlowService.selectPointFlowById(id));
    }

    /**
     * 新增积分流水
     */
    @PreAuthorize("@ss.hasPermi('point:pointFlow:add')")
    @Log(title = "积分流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointFlow pointFlow)
    {
        return toAjax(pointFlowService.insertPointFlow(pointFlow));
    }

    /**
     * 修改积分流水
     */
    @PreAuthorize("@ss.hasPermi('point:pointFlow:edit')")
    @Log(title = "积分流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PointFlow pointFlow)
    {
        return toAjax(pointFlowService.updatePointFlow(pointFlow));
    }

    /**
     * 删除积分流水
     */
    @PreAuthorize("@ss.hasPermi('point:pointFlow:remove')")
    @Log(title = "积分流水", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pointFlowService.deletePointFlowByIds(ids));
    }

    /**
     * 查询用户积分明细列表（多表联查）
     */
    @GetMapping("/detail-by-user/{userId}")
    public AjaxResult getDetailByUser(@PathVariable("userId") Long userId)
    {
        return success(pointFlowService.selectPointFlowDetailByUserId(userId));
    }

    /**
     * 查询积分明细列表（多表联查，支持分页和筛选）
     */
    @GetMapping("/detail-list")
    public TableDataInfo getDetailList(PointFlow pointFlow)
    {
        startPage();
        List<PointFlow> list = pointFlowService.selectPointFlowDetailList(pointFlow);
        return getDataTable(list);
    }

    /**
     * 查询用户积分统计信息
     */
    @GetMapping("/user-stats/{userId}")
    public AjaxResult getUserStats(@PathVariable("userId") Long userId)
    {
        Map<String, Object> stats = pointFlowService.selectUserPointStats(userId);
        return success(stats);
    }

    /**
     * 查询用户本月积分统计
     */
    @GetMapping("/user-month-stats/{userId}")
    public AjaxResult getUserMonthStats(@PathVariable("userId") Long userId)
    {
        Map<String, Object> stats = pointFlowService.selectUserMonthPointStats(userId);
        return success(stats);
    }

    /**
     * 查询积分流水完整信息列表（完整多表联查，支持分页和筛选）
     */
    @GetMapping("/complete-list")
    public TableDataInfo getCompleteList(PointFlow pointFlow)
    {
        startPage();
        List<PointFlow> list = pointFlowService.selectPointFlowCompleteList(pointFlow);
        return getDataTable(list);
    }

    /**
     * 查询积分流水完整信息列表（优化版本，解决数据查询不完整问题）
     */
    @GetMapping("/complete-list-optimized")
    public TableDataInfo getCompleteListOptimized(PointFlow pointFlow)
    {
        startPage();
        List<PointFlow> list = pointFlowService.selectPointFlowCompleteListOptimized(pointFlow);
        return getDataTable(list);
    }

    /**
     * 查询积分流水基础信息列表（最简化版本，确保返回所有数据）
     */
    @GetMapping("/basic-list")
    public TableDataInfo getBasicList(PointFlow pointFlow)
    {
        startPage();
        List<PointFlow> list = pointFlowService.selectPointFlowBasicList(pointFlow);
        return getDataTable(list);
    }

    /**
     * 查询用户积分流水完整信息（完整多表联查）
     */
    @GetMapping("/complete-by-user/{userId}")
    public AjaxResult getCompleteByUser(@PathVariable("userId") Long userId)
    {
        return success(pointFlowService.selectUserPointFlowComplete(userId));
    }

    /**
     * 根据积分规则类型查询相关流水（完整多表联查）
     */
    @GetMapping("/by-rule-type/{ruleType}")
    public AjaxResult getByRuleType(@PathVariable("ruleType") String ruleType)
    {
        List<PointFlow> list = pointFlowService.selectPointFlowByRuleType(ruleType);
        return success(list);
    }

    /**
     * 查询积分流水完整信息（包含所有关联数据）
     */
    @GetMapping("/complete-info/{userId}")
    public AjaxResult getCompleteInfo(@PathVariable("userId") Long userId)
    {
        List<PointFlow> list = pointFlowService.selectPointFlowCompleteInfo(userId);
        return success(list);
    }

    /**
     * 根据积分流水ID查询完整信息（包含所有关联数据）
     */
    @GetMapping("/complete-detail/{id}")
    public AjaxResult getCompleteDetail(@PathVariable("id") Long id)
    {
        PointFlow pointFlow = pointFlowService.selectPointFlowWithAllRelations(id);
        return success(pointFlow);
    }
}
